package com.originalpoint.origin_server.service.Impl;

import com.originalpoint.origin_server.mapper.LoginMapper;
import com.originalpoint.origin_server.pojo.Login;
import com.originalpoint.origin_server.result.LoginResult;
import com.originalpoint.origin_server.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class LoginServiceImpl implements LoginService {


    @Resource
    private  LoginMapper loginMapper;

    @Override
    public LoginResult login(Login login) {
        LoginResult result = new LoginResult();
        int role1=login.getRole();
        switch (login.getRole()) {
            case 1: // 管理员
                login = loginMapper.getAdminByCredentials(login);
                break;
            case 2: // 商家
                login = loginMapper.getMerchantByCredentials(login);
                break;
            case 3: // 用户
                login = loginMapper.getUserByCredentials(login);
                System.out.println(login);
                break;
        }

        if (login != null) {
            if(role1!=login.getRole()){
                result.setSuccess(true);
                result.setMessage("类型不匹配");

            }else {
                result.setSuccess(true);
                result.setMessage("登录成功");
                result.setUserId(login.getUserId()); // 添加userId字段
            }
        } else {
            result.setSuccess(false);
            result.setMessage("账号或密码错误");
        }

        return result;
    }
}
